package com.macbulldev.poc.testgin.domain;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.EntityManager;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.persistence.Version;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.transaction.annotation.Transactional;


@Configurable
@javax.persistence.Entity
@org.springframework.roo.addon.javabean.RooJavaBean
@org.springframework.roo.addon.tostring.RooToString
@org.springframework.roo.addon.entity.RooEntity
public class TrialCase {

    private String name;
    
    @Column(length=4000)
    private String calcSteps;
    
    @javax.persistence.ManyToOne(targetEntity = com.macbulldev.poc.testgin.domain.TrialGroup.class)
    @javax.persistence.JoinColumn(name="trialGroupId")
    private com.macbulldev.poc.testgin.domain.TrialGroup trialGroup;
    
    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "trialCase")
    private Set<LiabilityAmount> liabilityAmounts;
    
    private String createUsername;
    private String updateUsername;

    @javax.persistence.Temporal(javax.persistence.TemporalType.TIMESTAMP)
    @org.springframework.format.annotation.DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss.SSS")
    private java.util.Date createTs;
    
    @javax.persistence.Temporal(javax.persistence.TemporalType.TIMESTAMP)
    @org.springframework.format.annotation.DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss.SSS")
    private java.util.Date updateTs;
    
    public static List<TrialCase> findTrialCaseByGroup(Long groupId) {

    	EntityManager em = new TrialCase().entityManager; 
    	
		String hql="select tc from TrialCase tc where tc.trialGroup.id = ?";

		Query q = em.createQuery(hql);
    	
    	q.setParameter(1, groupId);
    	
        return q.getResultList();        

	}
    

	@PersistenceContext    
    transient EntityManager entityManager;

	@Id    
    @GeneratedValue(strategy = GenerationType.AUTO)    
    @Column(name = "id")    
    private Long id;

	@Version    
    @Column(name = "version")    
    private Integer version;

	public Long getId() {    
        return this.id;        
    }

	public void setId(Long id) {    
        this.id = id;        
    }

	public Integer getVersion() {    
        return this.version;        
    }

	public void setVersion(Integer version) {    
        this.version = version;        
    }

	@Transactional    
    public void persist() {    
        if (this.entityManager == null) this.entityManager = entityManager();        
        this.entityManager.persist(this);        
    }

	@Transactional    
    public void remove() {    
        if (this.entityManager == null) this.entityManager = entityManager();        
        if (this.entityManager.contains(this)) {        
            this.entityManager.remove(this);            
        } else {        
            TrialCase attached = this.entityManager.find(TrialCase.class, this.id);            
            this.entityManager.remove(attached);            
        }        
    }

	@Transactional    
    public void flush() {    
        if (this.entityManager == null) this.entityManager = entityManager();        
        this.entityManager.flush();        
    }

	@Transactional    
    public void merge() {    
        if (this.entityManager == null) this.entityManager = entityManager();        
        TrialCase merged = this.entityManager.merge(this);        
        this.entityManager.flush();        
        this.id = merged.getId();        
    }

	public static final EntityManager entityManager() {    
        EntityManager em = new TrialCase().entityManager;        
        if (em == null) throw new IllegalStateException("Entity manager has not been injected (is the Spring Aspects JAR configured as an AJC/AJDT aspects library?)");        
        return em;        
    }

	public static long countTrialCases() {    
        return (Long) entityManager().createQuery("select count(o) from TrialCase o").getSingleResult();        
    }

	public static List<TrialCase> findAllTrialCases() {    
        return entityManager().createQuery("select o from TrialCase o order by o.id asc").getResultList();        
    }

	public static TrialCase findTrialCase(Long id) {    
        if (id == null) throw new IllegalArgumentException("An identifier is required to retrieve an instance of TrialCase");        
        return entityManager().find(TrialCase.class, id);        
    }

	public static List<TrialCase> findTrialCaseEntries(int firstResult, int maxResults) {    
        return entityManager().createQuery("select o from TrialCase o order by o.id asc").setFirstResult(firstResult).setMaxResults(maxResults).getResultList();        
    }

	public String getName() {    
        return this.name;        
    }

	public void setName(String name) {    
        this.name = name;        
    }

	public String getCalcSteps() {    
        return this.calcSteps;        
    }

	public void setCalcSteps(String calcSteps) {    
        this.calcSteps = calcSteps;        
    }

	public TrialGroup getTrialGroup() {    
        return this.trialGroup;        
    }

	public void setTrialGroup(TrialGroup trialGroup) {    
        this.trialGroup = trialGroup;        
    }

	public List<LiabilityAmount> getLiabilityAmounts() {    
		EntityManager em = new LiabilityAmount().entityManager;
		return em.createQuery("select o from LiabilityAmount o where o.trialCase.id = ? order by o.premium asc").setParameter(1, this.id).getResultList();
		//return this.liabilityAmounts;        
    }

	public void setLiabilityAmounts(Set<LiabilityAmount> liabilityAmounts) {    
        this.liabilityAmounts = liabilityAmounts;        
    }

	public String getCreateUsername() {    
        return this.createUsername;        
    }

	public void setCreateUsername(String createUsername) {    
        this.createUsername = createUsername;        
    }

	public String getUpdateUsername() {    
        return this.updateUsername;        
    }

	public void setUpdateUsername(String updateUsername) {    
        this.updateUsername = updateUsername;        
    }

	public Date getCreateTs() {    
        return this.createTs;        
    }

	public void setCreateTs(Date createTs) {    
        this.createTs = createTs;        
    }

	public Date getUpdateTs() {    
        return this.updateTs;        
    }

	public void setUpdateTs(Date updateTs) {    
        this.updateTs = updateTs;        
    }

	public String toString() {    
        StringBuilder sb = new StringBuilder();        
        sb.append("Id: ").append(getId()).append(", ");        
        sb.append("Version: ").append(getVersion()).append(", ");        
        sb.append("Name: ").append(getName()).append(", ");                
        sb.append("CalcSteps: ").append(getCalcSteps()).append(", ");        
        sb.append("TrialGroup: ").append(getTrialGroup()).append(", ");        
        sb.append("LiabilityAmounts: ").append(getLiabilityAmounts() == null ? "null" : getLiabilityAmounts().size()).append(", ");        
        sb.append("CreateUsername: ").append(getCreateUsername()).append(", ");        
        sb.append("UpdateUsername: ").append(getUpdateUsername()).append(", ");        
        sb.append("CreateTs: ").append(getCreateTs()).append(", ");        
        sb.append("UpdateTs: ").append(getUpdateTs());        
        return sb.toString();        
    }
}
